﻿/* Cursor 扩展类
 */

/*待测试*/
namespace LSLib.Windows.Forms
{
    using System;
    using System.Drawing;
    using System.IO;
    using System.Windows.Forms;

    public class CursorEx
    {
        #region 获取鼠标指针GetCursor
        /// <summary>获取鼠标指针
        /// 如果图像文件不存在，则抛出异常。
        /// </summary>
        /// <param name="imgFile">图像文件(png最好)</param>
        public static Cursor GetCursor(string imgFile)
        {
            if (File.Exists(imgFile) == false) throw new FileNotFoundException("imgFile文件不存在！", imgFile);

            using (Bitmap bmp = new Bitmap(imgFile))
            {
                return GetCursor(bmp);
            }
        }

        /// <summary>获取鼠标指针
        /// 如果获取鼠标指针失败，则返回备用指针
        /// </summary>
        /// <param name="imgFile">图像文件(png最好)</param>
        /// <param name="reserveCursor">如果图像文件不存在，则返回备用指针</param>
        public static Cursor GetCursor(string imgFile, Cursor reserveCursor)
        {
            if (reserveCursor == null) reserveCursor = Cursors.Default;
            try
            {
                using (Bitmap bmp = new Bitmap(imgFile))
                {
                    return GetCursor(bmp);
                }
            }
            catch { return reserveCursor; }
        }

        /// <summary>获取鼠标指针
        /// </summary>
        /// <param name="bmp">位图</param>
        public static Cursor GetCursor(Bitmap bmp)
        {
            if (bmp == null) throw new ArgumentNullException("bmp");

            return new Cursor(bmp.GetHicon());
        }
        #endregion

        #region 设置系统鼠标指针
        /// <summary>设置系统鼠标指针
        /// </summary>
        /// <param name="CursorFile">鼠标指针文件*.cur</param>
        public static void SetSystemCursor(string cursorFile)
        {
            if (File.Exists(cursorFile) == false) throw new FileNotFoundException("cursorFile文件不存在！", cursorFile);

            IntPtr hcur = API.Window.LoadCursorFromFile(cursorFile);
            API.Window.SetSystemCursor(hcur, (uint)API.Window.OCR_SYSTEM_CURSORS.OCR_NORMAL);
        }
        #endregion
    }
}
